import {p} from './fn.js';
import {getAjax, postAjax} from './ajax.js';


/**
 * 
 * @param {*} url 
 * @param {*} layFilter 
 * @param {*} closeMyselfAndClickSearchButton 
 * @param {*} checkboxFields :checkbox类型，因为关闭不会有值，所以手动加上
 */
function saveDataOnListening(url, layFilter, closeMyselfAndClickSearchButton, checkboxFields) 
{    
    var layFilter = arguments[1] ? arguments[1] : 'lay-submit';
    var closeMyselfAndClickSearchButton = arguments[2] ? arguments[2] : 0;
    var checkboxFields = arguments[3] ? arguments[3] : null;

    // 表单
    form.on('submit('+ layFilter +')', function(data){
        postdata = data.field

        if (checkboxFields) {
            for(var i in checkboxFields) {
                var field = checkboxFields[i]
                if ( ! postdata[field]) {
                    postdata[field] = 0
                }
            }
        }

        var loading = layer.load(2, {
            shade: [0.3, '#fff']
        });

        postAjax(url, postdata, json => {
            layer.close(loading);

            if(json.code == 201){
                layer.msg(json.msg);
                return;
            }else{
                layer.msg(json.msg, {icon:6,time:1500},function(){});
                setTimeout(function() {
                    if (closeMyselfAndClickSearchButton) {
                        closeMyselfFrame()
                        clickMyParentSubmitButton()
                    }else {
                        // 窗口的父窗口刷新，当前窗口关闭
                        window.parent.location.reload();
                    }
                }, 1000)
            }
        })

        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
}

/**
 * 關閉自身：必須是子窗口
 */
function closeMyself()
{
    //当你在iframe页面关闭自身时
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭 
}

/**
 * 点击父窗口的搜索按钮
 */
function clickMyParentSubmitButton()
{
    // 子窗口中的代码
    var parentElement = window.parent.document.getElementById("submitBtn"); // 根据元素 ID 获取父窗口中的元素
    if (parentElement) {
        // 进行相关操作，比如修改样式、添加事件等
        parentElement.click()
    } 
}

function deleteOneTableData(url, data, layFilter) 
{
    layer.confirm('你确定要操作吗？', {icon: 7, title:'提示'}, function(index){
        layer.load(2)
        postAjax(url, data, res => {
            layer.closeAll()
            if(res.code == 201){
                layer.msg(res.msg, {icon: 5,time:1000});
                return;
            }else{
                layer.msg(res.msg, {icon:6,time:1000});
                // 刷新
                // $('#submitBtn').click()
                window.location.reload();
            }
        })
    });
}

function showTips(selector, text)
{
    layer.tips(text, selector, {tips:[2, '#663ff'], time:0});
}

function closeAllTips(selector, text)
{
    layer.closeAll('tips')
}

function getTipsTitle(field, tipsText, fieldTitle)
{
    var id = 'cols-'+field
    return '<span id="'+ id +'" onmouseleave="closeAllTips()" onmouseenter="showTips(\'#'+ id +'\', \''+ tipsText +'\')">'+ fieldTitle +'<i class="layui-icon layui-icon-help" style="color: #888;font-size: 13px;"></i></span>'
}




